<template>
  <el-container class="home-container">
  <el-header class="home-header">
    <div>
      <img src="../assets/heima.png" alt="">
      <span>商品后台管理</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <el-container>
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <!-- 按钮 -->
      <div class="button-menu" @click="toggleMenu">|||</div>
      <!-- 侧边栏菜单导航一 -->
        <el-menu
        :default-active='activePath'
        :router="true"
        :collapse-transition='false'
        :collapse='isCollapse'
        unique-opened
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#409bff">
      <!-- 一级菜单航 -->
      <el-submenu :index="item.order + ''" v-for="item in menuList" :key="item.order">
        <template slot="title">
          <!-- 一级菜单图标 -->
          <i :class="iconfontObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/' + subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
           <template slot="title">
          <!-- 二级菜单图标 -->
          <i class="el-icon-menu"></i>
          <span>{{subitem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconfontObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-lock_fill',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  // 在页面一上来立即发送请求获取导航菜单列表
  // 定义一个生命周期函数
  created() {
    this.getMennuList() // 调用该函数
    // 当页面刷新时，把本地存储的activepath值赋值给this.activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMennuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res.data)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggleMenu() {
      this.isCollapse = !this.isCollapse
      // console.log(this.width)
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
  .button-menu{
    color: #fff;
    text-align: center;
    line-height: 24px;
    height: 24px;
    letter-spacing: 2px;
    cursor: pointer;
  }
}
.el-main{
  background-color: #eadef1;
}
// 头部区域布局
.home-header{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  padding: 0,10px,0,0;
  > div{
    display: flex;
    align-items: center;
    span{
      padding-left: 10px;
      color: #fff;
      font-size: 20px;
      font-weight: bold;
    }
  }
}
.iconfont {
  margin-right: 10px
}
</style>
